#include "server.h"

#include <QtGui>

ServerInfo::ServerInfo() : port(21), proxyPort(21)
{
}

ServerInfo::ServerInfo(const ServerInfo &info) :
    host(info.host), user(info.user), pass(info.pass), dir(info.dir),
    proxyHost(info.proxyHost),
    port(info.port), proxyPort(info.proxyPort)
{
}

//Server::Server(QObject *parent) : QObject(parent), myConnId(-1)
//{
//  myFtp = new QFtp(this);
//  connect(myFtp, SIGNAL(commandFinished(int,bool)), this, SLOT(commandFinished(int,bool)));
//  connect(myFtp, SIGNAL(stateChanged(int)), this, SIGNAL(stateChanged(int)));
//}

Server::Server(QObject *parent) : QObject(parent), myConnId(-1)
{
  myHttp = new QHttp(this);

  //connect(myHttp, SIGNAL(requestFinished(int,bool)), this, SLOT(commandFinished(int,bool)));
  connect(myHttp, SIGNAL(done(bool)), this, SLOT(done(bool)));
  connect(myHttp, SIGNAL(stateChanged(int)), this, SIGNAL(stateChanged(int)));
}

void Server::setInfo(const ServerInfo &info)
{
  myInfo = info;
  myHttp->setHost(myInfo.host, QHttp::ConnectionModeHttp, myInfo.port);
  myHttp->setProxy(myInfo.proxyHost, myInfo.proxyPort);
}

//bool Server::connectFtp()
//{
//  myFtp->setProxy(myInfo.proxyHost, myInfo.proxyPort);
//  myFtp->setTransferMode(QFtp::Passive);
//
//  myCommandError = myCommandFinished = false;
//  myConnId = myFtp->connectToHost(myInfo.host, myInfo.port);
//
//  // wait for the connection
//  do {
//    QApplication::processEvents();
//
//    switch (myFtp->state())
//    {
//      case QFtp::Connected:
//        goto _login;
//      case QFtp::Closing:
//        return false;
//    }
//  } while (!myCommandError/* && !myCommandFinished*/);
//
//  if (myCommandError)
//      return false;
//
//_login:
//
//  myCommandError = myCommandFinished = false;
//  myConnId = myFtp->login(myInfo.user, myInfo.pass);
//
//  // wait for login
//  do {
//    QApplication::processEvents();
//
//    switch (myFtp->state())
//    {
//      case QFtp::LoggedIn:
//        return true;
//      case QFtp::Unconnected:
//      case QFtp::Closing:
//        return false;
//    }
//  } while (!myCommandError/* && !myCommandFinished*/);
//
//  return false;
//}
//
//int Server::getFile(const QString &serverPath, const QString &localPath, const QString &name)
//{
//  if (myFtp->state() != QFtp::LoggedIn)
//    return 0;
//
//  QFile file(localPath + "/" + name);
//  if (!file.open(QIODevice::WriteOnly))
//    return 0;
//
//  // binary getting
//  myCommandError = myCommandFinished = false;
//  myConnId = myFtp->get(serverPath + "/" + name, &file);
//
//  // wait for signal
//  while (!myCommandFinished)
//  {
//    QApplication::processEvents();
//  }
//
//  if (myCommandError)
//    return 0;
//
//  return file.size();
//}

//void Server::commandFinished(int, bool err)
//{
//  myCommandFinished = true;
//  myCommandError = err;
//}

void Server::done(bool err)
{
  myCommandFinished = true;
  myCommandError = err;
  qDebug() << myHttp->errorString();
}

bool Server::connectServer()
{
  myCommandError = myCommandFinished = false;
  return true;
}

int Server::getFile(const QString &serverPath, const QString &localPath, const QString &name)
{
  QFile file(localPath + "/" + name);
  if (!file.open(QIODevice::WriteOnly))
    return 0;

  // binary getting
  myCommandError = myCommandFinished = false;
  myConnId = myHttp->get(QUrl::toPercentEncoding(serverPath, "!$&'()*+,;=:@/"), &file);

  // wait for signal
  while (!myCommandFinished)
  {
    QApplication::processEvents();
  }

  file.flush();
  file.close();

  if (myCommandError)
    return 0;

  return file.size();
}
